cc.Class({
    extends: cc.Component,

    properties: {
        acc:40,//加速度
        dec:60,//减速度
        speed:0,//初始速度
        turnLeftBtn:{
            default:null,
            type:cc.Node
        },
        turnRightBtn:{
            default:null,
            type:cc.Node
        },
        accBtn:{
            default:null,
            type:cc.Node
        },
        decBtn:{
            default:null,
            type:cc.Node
        },
        tip:cc.Node,
        speedTxt:cc.Label
    },

    // use this for initialization
    onLoad: function () {
        //操作标识
        this.leftFlag = this.rightFlag = this.accFlag = this.decFlag = false;
        //获取人物刚体
        this.body = this.getComponent(cc.RigidBody);
        //绑定事件
        this.bindOperation();
    },

    bindOperation:function(){
        //向左旋转
        this.turnLeftBtn.on('touchstart',function(){
            if(!this.rightFlag){
                this.leftFlag = true;
            }
        }, this)
        this.turnLeftBtn.on('touchend',function(){
            this.leftFlag = false;
        }, this)
        this.turnLeftBtn.on('touchcancel',function(){
            this.leftFlag = false;
        }, this)
        //向右旋转
        this.turnRightBtn.on('touchstart',function(){
            if(!this.leftFlag){
                this.rightFlag = true;
            }
        }, this)
        this.turnRightBtn.on('touchend',function(){
            this.rightFlag = false;
        }, this)
        this.turnRightBtn.on('touchcancel',function(){
            this.rightFlag = false;
        }, this)
        //加速
        this.accBtn.on('touchstart',function(){
            if(!this.decFlag){
                this.accFlag = true;
            }
        }, this)
        this.accBtn.on('touchend',function(){
            this.accFlag = false;
        }, this);
        this.accBtn.on('touchcancel',function(){
            this.accFlag = false;
        }, this);
        //减速
        this.decBtn.on('touchstart',function(){
            if(!this.accFlag){
                this.decFlag = true;
            }
        }, this)
        this.decBtn.on('touchend',function(){
            this.decFlag = false;
        }, this);
        this.decBtn.on('touchcancel',function(){
            this.decFlag = false;
        }, this);
    },

    showTip:function(tip){
        this.tip.stopAllActions();
        this.tip.getChildByName("tip").getComponent(cc.Label).string = tip;
        this.tip.opacity = 255;
        this.tip.runAction(cc.fadeOut(1.0));
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(this.leftFlag){
            this.body.angularVelocity = 0;
            this.node.rotation -= 2;
        }
        if(this.rightFlag){
            this.body.angularVelocity = 0;
            this.node.rotation += 2;
        }
        if(this.accFlag){
            this.speed += dt * this.acc;
            this.speedTxt.string = parseInt(this.speed);
        }
        if(this.decFlag){
            let tmpSpeed = this.speed - dt * this.dec;
            this.speed = tmpSpeed > 0 ? tmpSpeed : 0;
            this.speedTxt.string = parseInt(this.speed);
        }

        let speed = {};
        speed.x = this.speed*Math.sin(this.node.rotation*Math.PI/180);
        speed.y = this.speed*Math.cos(this.node.rotation*Math.PI/180);
        this.body.linearVelocity = speed;
    },
});
